package com.ldodds.ot.util;

import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.vocabulary.RDF;

/**
 * Utility methods for manipulating Jena Statement objects
 * 
 * @author Leigh Dodds
 */
public class StatementUtils {

	public static boolean isObjectOfStatementAList(Statement statement) {
		RDFNode object = statement.getObject();
		if ( object.isLiteral() ) {
			return false;
		}
		Resource resource = (Resource)object;
		if ( resource.hasProperty(RDF.first) ) {
			return true;
		}
		return false;
	}
	
	public static boolean isObjectOfStatementACollection(Statement statement) {
		RDFNode object = statement.getObject();
		if ( object.isLiteral() ) {
			return false;
		}
		Resource resource = (Resource)object;
		if ( resource.hasProperty(RDF.type, RDF.Seq) ||
				resource.hasProperty(RDF.type, RDF.Bag) ||	
				resource.hasProperty(RDF.type, RDF.Alt)) {
			return true;
		}
		return false;
	}
	
	public static boolean isObjectOfStatementASequence(Statement statement) {
		return isObjectOfStatementOfType(statement, RDF.Seq);
	}

	public static boolean isObjectOfStatementAnAlt(Statement statement) {
		return isObjectOfStatementOfType(statement, RDF.Alt);
	}
	
	public static boolean isObjectOfStatementABag(Statement statement) {
		return isObjectOfStatementOfType(statement, RDF.Bag);
	}	
	
	public static boolean isObjectOfStatementOfType(Statement statement, Resource type) {
		RDFNode object = statement.getObject();
		if ( object.isLiteral() ) {
			return false;
		}
		Resource resource = (Resource)object;
		if ( resource.hasProperty(RDF.type, type) ) {
			return true;
		}
		return false;				
	}
}
